<?php


namespace app\controller;


use think\facade\Db;

class Car
{
    public function getList()
    {

        $req = Request();
        $openid = $req->openid;
        $status = $req->get("status", "0");

//        $carNum = input("carNum","");
        // 获取车辆列表

//        $sqlStr = "select * from 驾驶员信息表 where wx_h5_openid = $openid";


//        $list = Db::query($sqlStr);

        if ($status === '0') {
            $list = Db::table('驾驶员信息表')->where('wx_h5_openid', $openid)->select()->toArray();
        }

        if ($status === '1') {
            $list = Db::table('驾驶员信息表_his')->where('wx_h5_openid', $openid)->select()->toArray();
        }


        return json([
            'code' => 1,
            'msg' => 'ok',
            'data' => $list
        ]);
    }

    /**
     * 新增车辆
     * @return \think\response\Json
     *
     */
    public function addCar()
    {
        $req = Request();
        $openid = $req->openid;
        $postData = $req->post();

        $wechatUser = Db::table('Sys_WeChatInf')->where('wx_h5_openid', $openid)->find();
        if (!$wechatUser) {
            return json([
                'code' => -1,
                'msg' => '未找到此微信用户'
            ]);
        };

        // openid 查 Sys_WeChatInf 关联到account
        $res = Db::table("Sys_WeChatInf")->where("wx_h5_openid", $openid)->find();
        if (!$res) {
            return json([
                'code' => -1,
                'msg' => '未查到此微信用户'
            ]);
        }
        $Account = $res['Account'];
        // 再用account 查 JL_CZKeHuJL 获得用户
        $kehu = Db::table("JL_CZKeHuJL")->where("Account", $Account)->find();
        if (!$kehu) {
            return json([
                'code' => -1,
                'msg' => '未查到此客户记录'
            ]);
        }

        if ($kehu['type'] != '1') {
//            矿粉车辆  检查余顿
            $huowu = Db::table('JL_CZKeHuHuoWu')->where([
                'KeHuCode' => $kehu['BianHao'],
                'HWCode' => $postData['货物编号']
            ])->find();


            if (!$huowu) {
                return json([
                    'code' => -1,
                    'msg' => '未查到货物余吨信息'
                ]);
            }

            if (!$huowu['RemainTon'] > 0) {
                return json([
                    'code' => -1,
                    'msg' => '货物余吨不足'
                ]);
            }
        }

        // 查询车辆表 如果当前有这个车辆不能重复添加
        $chehao = $postData['车号'];
        $querySQL = "SELECT * FROM 驾驶员信息表 where 车号 = '$chehao'";
        $res = DB::query($querySQL);

        if (count($res)) {
            return json([
                'code' => -1,
                'msg' => '此车辆有未完成的称重订单'
            ]);
        }

        $Account = $wechatUser['Account'];
        $Name = $wechatUser['Name'];
        // dump($postData);
        $CardNo = $postData['卡号'];
        $Plate = $postData['车号'];
        $JSYName = $postData['驾驶员姓名'];
        $JSYPhone = $postData['联系电话'];
        $JZDCode = $postData['场区编号'];// 场区id
        $JZDName = $postData['场区名称'];// 场区名称
        $HuoWuCode = $postData['货物编号'];
        $HuoWuName = $postData['货物名称'];
        $GuiGeCode = $postData['产品规格编号'];//产品规格代码
        $GuiGeName = $postData['产品规格'];//产品规格名称
        $TypeCode = $postData["类型编码"];
        $TypeName = $postData["类型名称"];
        $SHDWCode = $postData["收货单位编号"];
        $SHDWName = $postData["收货单位"];
        $GHDWCode = $postData["供货单位编号"];
        $GHDWName = $postData["供货单位"];
        $Chandi = $postData["产地"];
        $ChandiCode = $postData["产地编号"];
        $beizhu = $postData["备注"];
        $queryStr = "?CardNo=$CardNo&Plate=$Plate&JSYName=$JSYName&JSYPhone=$JSYPhone&JZDCode=$JZDCode&JZDName=$JZDName&HuoWuCode=$HuoWuCode&HuoWuName=$HuoWuName&GuiGeCode=$GuiGeCode&GuiGeName=$GuiGeName&TypeCode=$TypeCode&TypeName=$TypeName&SHDWCode=$SHDWCode&SHDWName=$SHDWName&GHDWCode=$GHDWCode&GHDWName=$GHDWName&OpenID=$openid&BeiZhu=$beizhu&Chandi=$Chandi&ChandiCode=$ChandiCode&KeHuAccount=$Account&KeHuName=$Name";
        // dump("http://121.36.106.84:8070/TruckAdd/".$queryStr);
        $queryStr = encodeURI($queryStr);
        $url = "http://127.0.0.1:9001/TruckAdd/" . $queryStr;

        $res = httpGet($url);
        // dump($url);
        // dump($res);
        // die();
        return json([
            'code' => 1,
            'msg' => 'ok',
            'data' => $res,
            'url' => $url
        ]);
    }

    /**
     * 删除车辆
     */
    public function delCar()
    {
        $req = Request();
        $Plate = $req->post("车牌号");
        $queryStr = "Plate=$Plate";
        $queryStr = encodeURI($queryStr);
        $url = "http://127.0.0.1:9001/TruckDelete/?" . $queryStr;

        $res = httpGet($url);

        return json([
            'code' => 1,
            'msg' => 'ok',
            'data' => $res,
            'url' => $url
        ]);
    }
}